package com.huaweicloud.apm.example;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.annotation.JsonProperty;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;

public class DeleteApp {

    private static final String IAM_HEADER_TOEKN = "X-Auth-Token";

    private static final String IAM_TOKEN_VALUE = "{用户获取的token}";

    private static final String X_BUSINESS_ID = "x-business-id";

    private static final String OPEN_API_URL_DELETE_APP
        = "https://{endPoint}/v1/apm2/openapi/cmdb/apps/delete-app/application_id";

    private static final String APP_ID = "application_id";

    public static OkVO deleteApp(Long appId, Long xBusinessId) {
        HttpHeaders headers = new HttpHeaders();
        headers.add(IAM_HEADER_TOEKN, IAM_TOKEN_VALUE);
        headers.add(X_BUSINESS_ID, String.valueOf(xBusinessId));

        RestTemplate restTemplate = Utils.restTemplate();
        ResponseEntity<String> response = restTemplate.exchange(
            OPEN_API_URL_DELETE_APP.replaceAll(APP_ID, String.valueOf(appId)), HttpMethod.DELETE,
            new HttpEntity<String>(headers), String.class, new HashMap<>());
        String body = response.getBody();
        OkVO res = JSON.toJavaObject(JSON.parseObject(body), OkVO.class);
        return res;
    }

    public static class OkVO {

        @JsonProperty("ok")
        private String ok = "ok";

        public String getOk() {
            return ok;
        }

        public void setOk(String ok) {
            this.ok = ok;
        }

    }

}
